package timing.ukulele.third.service.msg;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import timing.ukulele.data.third.data.msg.MessageTemplateDTO;
import timing.ukulele.data.third.view.msg.MessageTemplateVO;
import timing.ukulele.third.convertor.msg.MessageTemplateConverter;
import timing.ukulele.third.exception.ThirdServiceException;
import timing.ukulele.third.mapper.msg.MessageTemplateMapper;
import timing.ukulele.third.persistent.msg.ChannelAccount;
import timing.ukulele.third.persistent.msg.ChannelAccountGroup;
import timing.ukulele.third.persistent.msg.MessageTemplate;
import timing.ukulele.third.service.common.BaseOperatorService;
import java.util.List;

/**
 * 消息模板
 */
@Slf4j
@Service
@AllArgsConstructor
public class MessageTemplateService extends BaseOperatorService<MessageTemplateMapper, MessageTemplate> {

    private final ChannelAccountService channelAccountService;

    private final ChannelAccountGroupService channelAccountGroupService;

    public Boolean addMessageTemplate(MessageTemplateDTO dto) {
        validateWriteOperation(dto);
        MessageTemplate messageTemplate = MessageTemplateConverter.INSTANCE.d2p(dto);
        return this.save(messageTemplate);
    }

    private void validateWriteOperation(MessageTemplateDTO dto) {
        if (this.lambdaQuery()
                .eq(MessageTemplate::getName, dto.getName())
                .notIn(dto.getId() != null, MessageTemplate::getId, dto.getId()).count() > 0) {
            throw new ThirdServiceException("模板名称已存在，请勿重复添加");
        }
        ChannelAccountGroup channelAccountGroup = channelAccountGroupService.getById(dto.getGroupId());
        if(channelAccountGroup != null) {
            dto.setGroupName(channelAccountGroup.getName());
        }
        ChannelAccount channelAccount = channelAccountService.getById(dto.getSendAccountId());
        if(channelAccount != null) {
            dto.setSendAccountName(channelAccount.getName());
        }
        // 处理接受者ID类型
    }

    public Boolean updateMessageTemplate(MessageTemplateDTO dto) {
        validateWriteOperation(dto);
        MessageTemplate messageTemplate = MessageTemplateConverter.INSTANCE.d2p(dto);
        return this.updateById(messageTemplate);
    }

    public Boolean deleteMessageTemplate(Long id) {
        return this.lambdaUpdate().set(MessageTemplate::getDeleted, Boolean.TRUE)
                .eq(MessageTemplate::getId, id).update();
    }

    public Boolean copy(Long id) {
        // 查询待复制数据
        MessageTemplate template = this.getById(id);
        // 创建复制对象并保存数据
        MessageTemplate clone = new MessageTemplate();
        BeanUtils.copyProperties(template, clone);
        clone.setId(null);
        return this.save(clone);
    }

    private MessageTemplate getAndValidate(Long id) {
        MessageTemplate messageTemplate = this.getById(id);
        if(messageTemplate == null) {
            throw new ThirdServiceException("消息模板数据不存在");
        }
        return messageTemplate;
    }

    public MessageTemplateVO getDetails(Long id) {
        MessageTemplate messageTemplate = getAndValidate(id);
        return MessageTemplateConverter.INSTANCE.p2v(messageTemplate);
    }

    public IPage<MessageTemplateVO> pageList(MessageTemplateDTO dto) {
        Page pageParam = new Page(dto.getCurrent(), dto.getPageSize());
        Page page = this.lambdaQuery()
                .eq(MessageTemplate::getGroupId, dto.getGroupId())
                .like(StrUtil.isNotBlank(dto.getName()), MessageTemplate::getName, dto.getName())
                .page(pageParam);
        List list = MessageTemplateConverter.INSTANCE.p2vList(page.getRecords());
        page.setRecords(list);
        return page;
    }

}
